package solutions.leetcode.normal

import solutions.lib.ListNode

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/swap-nodes-in-pairs/">24. 两两交换链表中的节点</a>
 */
class Solution24 {
    fun swapPairs(head: ListNode?): ListNode? {
        val root = ListNode()
        root.next = head
        var p: ListNode? = root
        while (p != null) {
            val next1 = p.next
            val next2 = next1?.next
            next2?.apply {
                p?.next = next2
                next1.next = next2.next
                next2.next = next1
            }
            p = next2?.next
        }
        return root.next
    }
}

fun main() {
    
}